/**
 * 代码生成管理初始化
 */
var beanFields = $("#beanFields");
var Code = {
		validateFields: {
			tableName: {
				validators: {
					notEmpty: {
						message: '表名不能为空'
					}
				}
			},
			path: {
				validators: {
					notEmpty: {
						message: '路径不能为空'
					}
				}
			},
			beanPackageName: {
				validators: {
					notEmpty: {
						message: 'bean包名不能为空'
					}
				}
			},
			beanName: {
				validators: {
					notEmpty: {
						message: 'bean类名不能为空'
					}
				}
			},
			daoPackageName: {
				validators: {
					notEmpty: {
						message: 'dao包名不能为空'
					}
				}
			},
			daoName: {
				validators: {
					notEmpty: {
						message: 'dao类名不能为空'
					}
				}
			},
			controllerPkgName: {
				validators: {
					notEmpty: {
						message: 'controller包名不能为空'
					}
				}
			},
			controllerName: {
				validators: {
					notEmpty: {
						message: 'controller类名不能为空'
					}
				}
			},
			servicePkgName: {
				validators: {
					notEmpty: {
						message: 'service包名不能为空'
					}
				}
			},
			serviceName: {
				validators: {
					notEmpty: {
						message: 'service类名不能为空'
					}
				}
			},
			jspath: {
				validators: {
					notEmpty: {
						message: 'js路径不能为空'
					}
				}
			},
			pagepath: {
				validators: {
					notEmpty: {
						message: '页面路径不能为空'
					}
				}
			},
			pagename: {
				validators: {
					notEmpty: {
						message: '页面名称不能为空'
					}
				}
			}
			

		}
};

/**
 * 验证数据是否为空
 */
Code.validate = function () {
	$('#codeInfoForm').data("bootstrapValidator").resetForm();
	$('#codeInfoForm').bootstrapValidator('validate');
	return $("#codeInfoForm").data('bootstrapValidator').isValid();
}

Code.validatetables = function () {
	$('#codeInfoDetailForm').data("bootstrapValidator").resetForm();
	$('#codeInfoDetailForm').bootstrapValidator('validate');
	return $("#codeInfoDetailForm").data('bootstrapValidator').isValid();
}
/**
 * 显示表字段
 */
Code.showtables = function () {
	if (!this.validate()) {

		return;
	}
	
     //提交信息
     var ajax = new $ax("/generate/detail/"+$("#tableName").val(), function(data){
       
     
         var beanName = data.beanName;
         $("#beanName").val(beanName);
         $("#daoName").val(beanName + "Dao");
         $("#controllerName").val(beanName + "Controller");
         $("#serviceName").val(beanName + "Service");
         var fields = data.fields;
         var length = fields.length;
        
         for(var i=0;i<length;i++){
             var f = fields[i];
           
             var tr = $("<tr></tr>");
             
             var columnName = f.columnName;
             var columnNameInput = "<input type='hidden' name='columnNames' id='columnNames' value='" + columnName +"'>"
             
             tr.append("<td>" + columnName + "</td>");
             tr.append("<td>" + f.columnType + "</td>");
             tr.append("<td>" + f.columnComment + "</td>");
             var name = f.name;
             var nameInput = "<input name='beanFieldName' id='beanFieldName' class='form-control' value='" + name +"'>";
             tr.append("<td>" + nameInput + "</td>");
             
             var type = f.type;
             var typeInput = "<input name='beanFieldType' id='beanFieldType' class='form-control' value='" + type +"'>"
             tr.append("<td>" + typeInput + "</td>");
             
             var val = f.columnDefault;
             var valInput = "<input name='beanFieldValue' id='beanFieldValue' class='form-control' value='" + val +"'>"
             tr.append("<td>" + valInput + "</td>");
             
             beanFields.append(tr);
         }
    
         EA.success("显示成功!");
         $("#detail").show();
         $("#save").show();
     },function(data){
     	EA.error("显示失败!" + data.responseJSON.message + "!");
     });
     ajax.start();

};




/**
 * 提交代码生成
 */
Code.generate = function () {

    
	
	if (!this.validate()) {

		return;
	}
	if (!this.validatetables()) {

		return;
	}
	
	//制作传送数据   
    columnNames=[];//字段名
    beanFieldName=[];//属性名
    beanFieldType=[];//变量类型
    beanFieldValue=[];//默认值
    $ ('#beanFields  tr').each (function (){  
    	var doms=$(this);
    	doms.children().each(function(i){
    	var td=$(this);
    
    	if(i==0){//字段名
    		columnNames.push($.trim(td.text()));//.text()获取td的文本内容，$.trim()去空格
    	}else if(i==2){//注释
    		
    	}else if(i==3){//属性名
    		beanFieldName.push($.trim(td.find("input").val()));
    	}else if(i==4){//类型
    		beanFieldType.push($.trim(td.find("input").val()));
    	}else if(i==5){//默认值
    		beanFieldValue.push($.trim(td.find("input").val()));
    	}
    	
    	})
     });

	
	
     $.post("/generate/save",{
    	 "pagepath":$("#pagepath").val(),
    	 "jspath":$("#jspath").val(),
    	 "pagename":$("#pagename").val(),
    	 "tableName":$("#tableName").val(),
    	 "path":$("#path").val(),
    	 "beanPackageName":$("#beanPackageName").val(),
    	 "beanName":$("#beanName").val(),
    	 "daoPackageName":$("#daoPackageName").val(),
    	 "daoName":$("#daoName").val(),
    	 "controllerPkgName":$("#controllerPkgName").val(),
    	 "controllerName":$("#controllerName").val(),
    	 "servicePkgName":$("#servicePkgName").val(),
    	 "serviceName":$("#serviceName").val(),
    	 "dbcolumns":columnNames.join(";"),
    	 "javabeanName":beanFieldName.join(";"),
    	 "javabeanType":beanFieldType.join(";"),
    	 "javabeanVaule":beanFieldValue.join(";")
     },function(data){
       $("#detail").hide();
       $("#save").hide();
       EA.success("生成成功!");  
     });
	
	
};
$(function () {
	//验证表单
	EA.initValidator("codeInfoForm", Code.validateFields);
	EA.initValidator("codeInfoDetailForm", Code.validateFields);
	$("#detail").hide();
	$("#save").hide();

});